package com.hangzhou.xyj.ui.fragment;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.type.EListType;
import com.hangzhou.xyj.ui.adapter.HomeSingVPAdapter;
import com.hangzhou.xyj.ui.adapter.SingDramaAdapter;
import com.hangzhou.xyj.ui.fragment.base.BaseFragment;
import com.hangzhou.xyj.ui.widget.PagerSlidingTabStrip;
import com.hangzhou.xyj.ui.widget.views.MyViewPager;
import com.hangzhou.xyj.utils.AnimationController;

import java.util.ArrayList;
import java.util.List;

/**
 * 唱戏演唱
 *
 * @author neo.duan
 */
public class SingFragment extends BaseFragment implements OnClickListener {
    public static final int LIST_TYPE = 0;
    public static final int DRAMA_TYPE = 1;
    public int mCurrentShowType = LIST_TYPE; //默认当前显示榜单
    TopListFragment topListProfessFragment;
    TopListFragment topListDramaFriendsFragment;
    private LinearLayout mSingListLayout;
    private LinearLayout mDramaLayout;
    private PagerSlidingTabStrip mSingListTitleTab;
    private PagerSlidingTabStrip mDramaTitleTab; //点戏tab
    private ViewPager mSingListViewPager;
    private MyViewPager mDramaViewPager;
    private LinearLayout mLlListLayout; //榜单条
    private Button mBtMainList;//总榜按钮
    private Button mBtMonthList;//月榜按钮
    private Button mBtWeekList;//周榜按钮
    private HomeSingVPAdapter mHomeSingVPAdapter;

    public SingFragment() {
    }

    @Override
    public void initTop() {
        enableTop(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.fragment_sing);
    }

    @Override
    public void initViews() {
        //初始化榜单页面
        mSingListLayout = findView(R.id.sing_list_layout);
        mSingListTitleTab = findView(R.id.sing_list_tabs);
        mSingListViewPager = findView(R.id.sing_list_viewpager);
        mLlListLayout = findView(R.id.ll_sing_list_root);

        //初始化点戏页面
        mDramaLayout = findView(R.id.sing_drama_layout);
        mDramaTitleTab = findView(R.id.sing_drama_tabs);
        mDramaViewPager = findView(R.id.sing_drama_viewpager);

        mBtMainList = findView(R.id.btn_main_list);
        mBtMonthList = findView(R.id.btn_month_list);
        mBtWeekList = findView(R.id.btn_week_list);
    }

    @Override
    public void initDatas() {
        //1.榜单数据
        Bundle bundle1 = new Bundle();
        topListProfessFragment = new TopListFragment();//榜单-专业Fragment
        bundle1.putInt("type", TopListFragment.TYPE_PROFESSIONAL);
        topListProfessFragment.setArguments(bundle1);
        topListProfessFragment.setCurrentListType(EListType.MAIN);
        setButtonListSelected(EListType.MAIN);

        Bundle bundle2 = new Bundle();
        topListDramaFriendsFragment = new TopListFragment();//榜单-戏友Fragment
        bundle2.putInt("type", TopListFragment.TYPE_DRAMA_FRIENDS);
        topListDramaFriendsFragment.setArguments(bundle2);
        topListDramaFriendsFragment.setCurrentListType(EListType.MAIN);

        List<TopListFragment> fragmentList = new ArrayList<>();
        fragmentList.add(topListProfessFragment);
        fragmentList.add(topListDramaFriendsFragment);

        //填充Fragment
        mSingListViewPager.setAdapter(mHomeSingVPAdapter = new HomeSingVPAdapter(getFragmentManager(), fragmentList));

        //向ViewPager绑定PagerSlidingTabStrip
        mSingListTitleTab.setViewPager(mSingListViewPager);
        mSingListViewPager.setCurrentItem(0);
        mSingListViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        setButtonListSelected(topListProfessFragment.getCurrentListType());
                        break;
                    case 1:
                        setButtonListSelected(topListDramaFriendsFragment.getCurrentListType());
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        //2.点戏数据
        mDramaViewPager.setAdapter(new SingDramaAdapter(getFragmentManager()));
        mDramaTitleTab.setViewPager(mDramaViewPager);
        mDramaViewPager.setCurrentItem(0);
        mDramaViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }

            @Override
            public void onPageSelected(final int position) {
                if (position == 2) {
                    mDramaViewPager.setCanLeft(false);//不能左滑
                } else {
                    mDramaViewPager.setCanLeft(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 显示哪个Layout
     *
     * @param type
     */
    public void showLayout(int type) {
        //当前显示就是要显示的，则不操作
        if (type == mCurrentShowType) {
            return;
        }
        switch (type) {
            case DRAMA_TYPE:
                mCurrentShowType = DRAMA_TYPE;
                mSingListLayout.setVisibility(View.GONE);
                mDramaLayout.setVisibility(View.VISIBLE);
                break;
            default:
                mCurrentShowType = LIST_TYPE;
                mSingListLayout.setVisibility(View.VISIBLE);
                mDramaLayout.setVisibility(View.GONE);
                break;

        }
    }

    @Override
    public void initListeners() {
        mBtMainList.setOnClickListener(this);
        mBtMonthList.setOnClickListener(this);
        mBtWeekList.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_list://总榜
                setButtonListSelected(EListType.MAIN);
                if (mSingListViewPager.getCurrentItem() == 0) {
                    topListProfessFragment.updateData(EListType.MAIN);
                    topListProfessFragment.setCurrentListType(EListType.MAIN);
                } else {
                    topListDramaFriendsFragment.updateData(EListType.MAIN);
                    topListDramaFriendsFragment.setCurrentListType(EListType.MAIN);
                }
                break;
            case R.id.btn_month_list://月榜
                setButtonListSelected(EListType.MONTH);
                if (mSingListViewPager.getCurrentItem() == 0) {
                    topListProfessFragment.updateData(EListType.MONTH);
                    topListProfessFragment.setCurrentListType(EListType.MONTH);
                } else {
                    topListDramaFriendsFragment.updateData(EListType.MONTH);
                    topListDramaFriendsFragment.setCurrentListType(EListType.MONTH);
                }
                break;
            case R.id.btn_week_list://周榜
                setButtonListSelected(EListType.WEEK);
                if (mSingListViewPager.getCurrentItem() == 0) {
                    topListProfessFragment.updateData(EListType.WEEK);
                    topListProfessFragment.setCurrentListType(EListType.WEEK);
                } else {
                    topListDramaFriendsFragment.updateData(EListType.WEEK);
                    topListDramaFriendsFragment.setCurrentListType(EListType.WEEK);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //榜单条动画
        AnimationController.slideIn(AnimationController.DIRECTION_UP, mLlListLayout, 800, 0);
    }

    /**
     * 设置哪个按钮被选中
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/4/18 17:36
     */
    public void setButtonListSelected(EListType type) {
        switch (type) {
            case MAIN:
                mBtWeekList.setSelected(false);
                mBtMonthList.setSelected(false);
                mBtMainList.setSelected(true);
                break;
            case MONTH:
                mBtWeekList.setSelected(false);
                mBtMonthList.setSelected(true);
                mBtMainList.setSelected(false);
                break;
            case WEEK:
                mBtWeekList.setSelected(true);
                mBtMonthList.setSelected(false);
                mBtMainList.setSelected(false);
                break;
        }
    }
}
